package com.neu.controller;

import com.neu.constant.MaterialTypeEnum;
import com.neu.constant.ResultCodeEnum;
import com.neu.pojo.Material;
import com.neu.service.MaterialService;
import com.neu.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping(value = "/material", produces = {"application/json;charset=UTF-8"})
public class MaterialController {

    @Resource
    private MaterialService materialService;

    @PostMapping("/i")
    public Object add(@RequestBody Material material) {
        log.info("新增文件资料:{}", material);
        return materialService.insertMaterial(material);
    }

    @PostMapping("/w")
    public Object update(@RequestBody Material material) {
        log.info("修改文件资料信息:{}", material);
        return materialService.updateMaterial(material);
    }

    @PostMapping("/d")
    public Object del(Integer id) {
        log.info("删除文件资料:{}", id);
        if (id <= 0) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return materialService.deleteMaterial(id);
    }

    @GetMapping("/list")
    public Object list(@RequestParam("pid") int pid, @RequestParam("type") int type) {
        log.info("查询文件资料 pid:{},type:{}", pid, type);
        if (pid <= 0 || MaterialTypeEnum.of(type) == MaterialTypeEnum.UNKNOWN) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return materialService.list(pid, type);
    }

}
